const router = require('express').Router();
const Movies = require('../models/movies');

/* router.get('/', async (req, res) => {
    const movies = await Movies.find({}).sort({
        _id: -1
    });
    res.render('movies/index', {
        movies,
        title: '经典影片'
    });
}) */
router.get('/', async (req, res) => {
    const page = req.query.page * 1 || 1;
    const per = req.query.per * 1 || 10;
    const movies = await Movies.find({}).sort({
        _id: -1
    }).skip((page - 1) * per).limit(per);
    const totals = await Movies.count();
    res.json({
        totals,
        pages: Math.ceil(totals / per),
        movies,
    });


});

module.exports = router;